package com.ymatou.c2cliveindexer.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.c2cliveindexer.parameter.LiveSearchReturnBean;
import com.ymatou.c2cliveindexer.parameter.LiveindexerBean;
import com.ymatou.c2cliveindexer.service.LiveSearchCall;
import com.ymatou.c2cliveindexer.service.LiveindexerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.AppRuleServiceActivityWeightScore;
import com.ymttest.database.model.YmtC2CLiveActivity;
import com.ymttest.database.sqlwapper.AppRuleServiceActivityWeightScoreWapper;
import com.ymttest.database.sqlwapper.YmtC2CLiveActivityWapper;

/**
 * c2clive搜索首页列表接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_LiveRegionSearch {
	static LiveindexerBean liveindexerBean;
	private static LiveindexerCall liveindexerCall;
	private static LiveSearchCall liveSearchCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("c2clive搜索地区活动列表接口");
		try {
			liveindexerBean = new LiveindexerBean();
			liveindexerCall = new LiveindexerCall();
			liveindexerBean.setAction("reload");
			liveindexerCall.setData(liveindexerBean);
			liveindexerCall.callService();
			Logger.debug("发送报文:" + liveindexerCall.getEntityString());
			Logger.debug("返回报文:" + liveindexerCall.getReturnData());
			Thread.sleep(5000);
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		liveSearchCall = new LiveSearchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(liveindexerCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LiveRegionSearch_001() {
		Logger.start(true,"搜索地区活动列表成功");
		try {
			// 1:北美，2：欧洲，3：亚洲，4：澳新
			int countryGroupId = 2;
			liveSearchCall
					.setData("query=term(countryGroupId,"
							+ countryGroupId
							+ ")&fl=id,isReplay,activityName,content,location,picUrl,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,500&sort=desc(yscore)");
			liveSearchCall.callService();
			Logger.comment("发送报文:" + liveSearchCall.getEntityString());
			Logger.comment("返回报文:" + liveSearchCall.getReturnData());
			Logger.verifyEquals("OK", liveSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			Map m = new HashMap();
			m.put("CountryGroupId", countryGroupId);
			// m.put("LiveActivityId", 2013);
			List<Map> list = new YmtC2CLiveActivityWapper().getsLiveActivity(m);
			List<LiveSearchReturnBean> returnList = liveSearchCall.getHitsList();
			// //升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(liveSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).get("id"), returnList.get(i).getId(), "验证数据库与返回的activityid");
					Logger.verifyEquals(list.get(i).get("activityName"), returnList.get(i).getActivityName(),
							"验证数据库与返回的activityName");
					Logger.verifyEquals(((String) list.get(i).get("content")).trim(), returnList.get(i).getContent()
							.trim(), "验证数据库与返回的content");
					Logger.verifyEquals(((String) list.get(i).get("location")).trim(), returnList.get(i).getLocation()
							.trim(), "验证数据库与返回的location");
					Logger.verifyEquals(list.get(i).get("picUrl"), returnList.get(i).getPicUrl(),
							"验证数据库与返回的picUrl");
					Logger.verifyEquals(list.get(i).get("brands"), returnList.get(i).getBrands(),
							"验证数据库与返回的barands");
					Logger.verifyEquals(list.get(i).get("sellerName"), returnList.get(i).getSellerName(),
							"验证数据库与返回的sellerName");
					Logger.verifyEquals(list.get(i).get("countryId"), returnList.get(i).getCountryId(),
							"验证数据库与返回的countryId");
				}
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LiveRegionSearch_002() {
		Logger.start(true,"搜索地区活动列表成功");
		try {
			// 1:北美，2：欧洲，3：亚洲，4：澳新
			int countryGroupId = 3;
			liveSearchCall
					.setData("query=term(countryGroupId,"
							+ countryGroupId
							+ ")&fl=id,isReplay,activityName,content,location,picUrl,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,10000&sort=desc(yscore)");
			liveSearchCall.callService();
			Logger.comment("发送报文:" + liveSearchCall.getEntityString());
			Logger.comment("返回报文:" + liveSearchCall.getReturnData());
			Logger.verifyEquals("OK", liveSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			Map m = new HashMap();
			m.put("CountryGroupId", countryGroupId);
			// m.put("LiveActivityId", 2013);
			List<Map> list = new YmtC2CLiveActivityWapper().getsLiveActivity(m);
			List<LiveSearchReturnBean> returnList = liveSearchCall.getHitsList();
			// //升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(liveSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).get("id"), returnList.get(i).getId(), "验证数据库与返回的activityid");
					Logger.verifyEquals(list.get(i).get("activityName"), returnList.get(i).getActivityName(),
							"验证数据库与返回的activityName");
					// Logger.verifyEquals(((String)
					// list.get(i).get("content")).trim(),
					// returnList.get(i).getContent().trim(),
					// "验证数据库与返回的content");
					Logger.verifyEquals(((String) list.get(i).get("location")).trim(), returnList.get(i).getLocation()
							.trim(), "验证数据库与返回的location");
					Logger.verifyEquals(list.get(i).get("picUrl"), returnList.get(i).getPicUrl(),
							"验证数据库与返回的picUrl");
					Logger.verifyEquals(list.get(i).get("brands"), returnList.get(i).getBrands(),
							"验证数据库与返回的barands");
					Logger.verifyEquals(list.get(i).get("sellerName"), returnList.get(i).getSellerName(),
							"验证数据库与返回的sellerName");
					Logger.verifyEquals(list.get(i).get("countryId"), returnList.get(i).getCountryId(),
							"验证数据库与返回的countryId");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LiveRegionSearch_003() {
		Logger.start(true,"搜索不存在的地区");
		try {
			// 1:北美，2：欧洲，3：亚洲，4：澳新
			int countryGroupId = 5;
			liveSearchCall
					.setData("query=term(countryGroupId,"
							+ countryGroupId
							+ ")&fl=id,isReplay,activityName,content,location,picUrl,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,100&sort=desc(yscore)");
			liveSearchCall.callService();
			Logger.comment("发送报文:" + liveSearchCall.getEntityString());
			Logger.comment("返回报文:" + liveSearchCall.getReturnData());
			Logger.verifyEquals("OK", liveSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			Map m = new HashMap();
			m.put("CountryGroupId", countryGroupId);
			// m.put("LiveActivityId", 2013);
			List<Map> list = new YmtC2CLiveActivityWapper().getsLiveActivity(m);
			List<LiveSearchReturnBean> returnList = liveSearchCall.getHitsList();
			// //升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(liveSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).get("id"), returnList.get(i).getId(), "验证数据库与返回的activityid");
					Logger.verifyEquals(list.get(i).get("activityName"), returnList.get(i).getActivityName(),
							"验证数据库与返回的activityName");
					Logger.verifyEquals(((String) list.get(i).get("content")).trim(), returnList.get(i).getContent()
							.trim(), "验证数据库与返回的content");
					Logger.verifyEquals(((String) list.get(i).get("location")).trim(), returnList.get(i).getLocation()
							.trim(), "验证数据库与返回的location");
					Logger.verifyEquals(list.get(i).get("picUrl"), returnList.get(i).getPicUrl(),
							"验证数据库与返回的picUrl");
					Logger.verifyEquals(list.get(i).get("brands"), returnList.get(i).getBrands(),
							"验证数据库与返回的barands");
					Logger.verifyEquals(list.get(i).get("sellerName"), returnList.get(i).getSellerName(),
							"验证数据库与返回的sellerName");
					Logger.verifyEquals(list.get(i).get("countryId"), returnList.get(i).getCountryId(),
							"验证数据库与返回的countryId");
				}
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// @Test
	@Category(P1.class)
	@TestCase
	public void Tc_LiveRegionSearch_004() {
		Logger.start(true,"搜索排序成功");
		try {
			int start = 0;
			int limit = 500;
			// 查询数据库符合条件的数据
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			// 在[App_RuleService_ActivityWeightScore] 排序规则表里造一条数据
			if (list.size() > 0) {
				int index = new Random().nextInt(list.size() - 1);
				Integer sellerId = list.get(index).getIsellerid();
				Integer activityId = list.get(index).getIactivityid();
				Integer countryId = list.get(index).getIcountryid();
				AppRuleServiceActivityWeightScore record = new AppRuleServiceActivityWeightScore();
				record.setIsellerid(sellerId);
				record.setIactivityid(activityId);
				record.setIlivecountry(countryId);
				record.setIcategoryid(3);
				record.setIsort(15);
				record.setDbegintime(new Date());
				record.setFra(new BigDecimal(1));
				record.setFrb(new BigDecimal(1));
				record.setFrc(new BigDecimal(1));
				record.setFrd(new BigDecimal(1));
				record.setFre(new BigDecimal(1));
				record.setDlastupdatetime(new Date());
				record.setBisreplay(0);
				// 在[App_RuleService_ActivityWeightScore] 排序规则表里造一条数据
				new AppRuleServiceActivityWeightScoreWapper().insert(record);
				Thread.sleep(3000);
				liveSearchCall
						.setData("fl=id,isReplay,activityName,content,location,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit="
								+ start + "," + limit + "&sort=desc(yscore)");
				liveSearchCall.callService();
				Logger.comment("发送报文:" + liveSearchCall.getEntityString());
				Logger.comment("返回报文:" + liveSearchCall.getReturnData());
				Logger.verifyEquals("OK", liveSearchCall.getStatus(), "验证返回状态");
				// 验证数据库
				ArrayList<LiveSearchReturnBean> returnList = liveSearchCall.getHitsList();
				// 升序排序
				// Collections.sort(returnList);
				Logger.verifyEquals(liveSearchCall.getTotal(), returnList.size(), "验证总条数");
				Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
				if ((returnList.size() > 0) && (list.size() == returnList.size())) {
					Logger.verifyEquals(activityId, returnList.get(0).getId(), "验证该直播" + activityId + "排在第一位");
					for (int i = 0; i < list.size(); i++) {

					}
				}
			} else {
				Logger.comment("没有合适的数据");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}